<?php  
namespace app\api\controller;

use think\Request;
use app\api\logic\Weixin as WeixinLogic;

/**
 * @SWG\Swagger(
 *     schemes={"https","http"},
 *     host="bike.wei-yong.com/api/Xixiche",
 *     consumes={"multipart/form-data"},
 *     produces={"application/json"},
 *     @SWG\Info(
 *       version="1.0",
 *       title="斑马汽车服务",
 *       description="洗洗车接口文档<br> 以后就在这里愉快的对接口把!<br>"
 *     ),
 *
 *     @SWG\Tag(
 *       name="Master",
 *       description="师傅端",
 *     ),
 *     
 *     @SWG\Tag(
 *       name="User",
 *       description="用户端",
 *     ),
 *
 * )
 */

class Xixiche extends Base
{
    /**
    * @SWG\Post(
    *   path="/user_type",
    *   tags={"Master"},
    *   summary="用户登陆验证",
    *   description="用户登陆验证 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="openid", description="用户OPENID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="member_status", type="string", description="用户类型：{0=用户，1=师傅}"),
    *       @SWG\Header(header="status", type="string", description="用户是否拥有月卡：{0=没有，1=有}"),
    *   )
    * )
    */
	public function user_type()
	{
		$where['id'] = $this->accessData['uid'];

		$res = db('user')->where($where)->field('status')->find();
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}

	}

    /**
    * @SWG\Post(
    *   path="/login",
    *   tags={"Master"},
    *   summary="师傅端 - 登陆",
    *   description="师傅端 - 登陆 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="openid", description="用户OPENID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="account", description="账号", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="password", description="密码", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *   )
    * )
    */
	public function login()
	{
		$post = request()->only(['account','password'],'post');

		if (empty($post['account'])) {
			err('请输入账号');
		}elseif (empty($post['password'])) {
			err('请输入密码');
		}

		$where['id'] = $this->accessData['uid'];

		$where['member_status'] = 1;
		$res = db('user')->where($where)->find();

		if (!empty($res)) {
			if ($res['account'] == $post['account']) {
				if ($res['password'] == $post['password']) {
					suc('登陆成功');
				}else{
					err('密码错误');
				}
			}else{
				err('账号错误');
			}
		}else{
			err('该用户不存在');
		}
	}

    /**
    * @SWG\Post(
    *   path="/payment",
    *   tags={"Master"},
    *   summary="师傅端 - 未完成订单",
    *   description="师傅端 - 未完成订单 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="订单ID"),
    *       @SWG\Header(header="name", type="string", description="姓名"),
    *       @SWG\Header(header="gender", type="string", description="性别"),
    *       @SWG\Header(header="mobile", type="string", description="电话"),
    *       @SWG\Header(header="address", type="string", description="地址"),
    *       @SWG\Header(header="title", type="string", description="项目"),
    *   )
    * )
    */
	public function payment()
	{
        $post = request()->only(['num','limit'], 'post');
		$where['status'] = [['=','2'],['=','3'],'or'];
        $where['order_type'] = 2;
		$where['user_info_id'] = $this->accessData['uid'];
		$field = ['id,name,mobile,gender,address,title,car_id,headr_img,time,status,hour'];
		$res = db('xixiche_order')->where($where)->field($field)->order('id desc')->limit($post['num'], $post['limit'])->select();
		foreach ($res as $key => $val) {
			$res[$key]['time'] = date("Y-m-d", $val['time']). " " . $val['hour'];
		}
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}
	
    /**
    * @SWG\Post(
    *   path="/wash_img",
    *   tags={"Master"},
    *   summary="师傅端 - 提交洗车照片",
    *   description="师傅端 - 提交洗车照片 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="订单ID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="wash_start_img", description="洗车前照片", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="wash_end_img", description="洗车后照片", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *   )
    * )
    */
	public function wash_img()
	{
		$post = Request::instance()->param();
		if (empty($post['id'])) {
			err('请提交订单ID');
		}
		if (isset($post['wash_start_img'])) {
			$data['start_img'] = json_encode($post['wash_start_img']);
			$data['status'] = 3;
			$data['start_time'] = time();
			$res = db('xixiche_order')->where(['id'=>$post['id']])->update($data);
			$info = db('xixiche_order')->where(['id'=>$post['id']])->field('status')->find();
			if ($res) {
				suc('提交成功', $info);
			}else{
				err('提交失败');
			}
		}elseif (isset($post['wash_end_img'])) {
			$data['end_img'] = json_encode($post['wash_end_img']);
			$data['status'] = 4;
            $data['end_time'] = time();
			$res = db('xixiche_order')->where(['id'=>$post['id']])->update($data);
			$info = db('xixiche_order')->where(['id'=>$post['id']])->field('status')->find();
			if ($res) {
				suc('提交成功', $info);
			}else{
				err('提交失败');
			}
		}else{
			err('请提交照片');
		}
	}
    /**
    * @SWG\Post(
    *   path="/see_car",
    *   tags={"Master"},
    *   summary="师傅端 - 查看车辆信息",
    *   description="师傅端 - 查看车辆信息 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="订单ID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="title", type="string", description="汽车品牌型号"),
    *       @SWG\Header(header="number", type="string", description="车牌"),
    *       @SWG\Header(header="color", type="string", description="颜色"),
    *   )
    * )
    */
	public function see_car()
	{
		$post = request()->only(['id','post']);

		// $res =  db('xixiche_order')->where(['id'=>$post['id']])->find();
		
		$car = db('xixiche_car')->where(['id'=>$post['id']])->field('title,number,color')->find();

		if ($car) {
			suc('', $car);
		}else{
			err('没有数据');
		}

	}
    /**
    * @SWG\Post(
    *   path="/paid",
    *   tags={"Master"},
    *   summary="师傅端 - 已完成订单",
    *   description="师傅端 - 已完成订单 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="time", description="时间", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="订单ID"),
    *       @SWG\Header(header="name", type="string", description="姓名"),
    *       @SWG\Header(header="gender", type="string", description="性别"),
    *       @SWG\Header(header="mobile", type="string", description="电话"),
    *       @SWG\Header(header="address", type="string", description="地址"),
    *       @SWG\Header(header="title", type="string", description="项目"),
    *   )
    * )
    */
	public function paid()
	{
		$post = request()->only(['time','num','limit'],'post');

 		if (empty($post['time'])) {
 			err('请提交时间');
 		}
		$start_time = strtotime($post['time']);
		$end_time = $start_time + 86399;
		
		$where['status'] = 4;
        $where['order_type'] = 2;
		$where['user_info_id'] = $this->accessData['uid'];
		$where['end_time'] = ['between',[$start_time,$end_time]];

		$field = ['id,name,mobile,gender,address,title,headr_img,time,hour,user_id'];
		$res = db('xixiche_order')->where($where)->field($field)->order('id desc')->limit($post['num'], $post['limit'])->select();
		foreach ($res as $key => $val) {
			$res[$key]['time'] = date("Y-m-d", $val['time']). " " . $val['hour'];
            $info = db('user')->where(['id'=>$val['user_id']])->field('avatarurl')->find();
            $res[$key]['avatarurl'] = $info['avatarurl'];
		}

		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}

    /**
    * @SWG\Post(
    *   path="/order_details",
    *   tags={"Master"},
    *   summary="师傅端 - 订单详情",
    *   description="师傅端 - 订单详情 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="订单ID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="order_name", type="string", description="姓名"),
    *       @SWG\Header(header="order_gender", type="string", description="性别"),
    *       @SWG\Header(header="order_mobile", type="string", description="电话"),
    *       @SWG\Header(header="order_address", type="string", description="地址"),
    *       @SWG\Header(header="order_title", type="string", description="项目"),
    *       
    *       @SWG\Header(header="car_title", type="string", description="汽车品牌型号"),
    *       @SWG\Header(header="car_number", type="string", description="车牌"),
    *       @SWG\Header(header="car_color", type="string", description="颜色"),
    *       
    *       @SWG\Header(header="comment_content", type="string", description="评价内容"),
    *       @SWG\Header(header="comment_grade", type="string", description="评价星级"),
    *       @SWG\Header(header="comment_create_time", type="string", description="评价时间"),
    *   )
    * )
    */
	public function order_details()
	{
		$post = request()->only(['id','post']);
		//订单详情
		$field = ['name,gender,mobile,address,title,car_id,user_id,end_time'];
		$res['order'] = db('xixiche_order')->where(['id'=>$post['id']])->field($field)->find();
		$res['order']['end_time'] = date("Y-m-d H:i", $res['order']['end_time']);
		// $res['order']['start_img'] = json_decode($res['order']['start_img']);
		// $res['order']['end_img'] = json_decode($res['order']['end_img']);
		//车辆信息
		$res['car'] = db('xixiche_car')->where(['id'=>$res['order']['car_id']])->field('title,number,color')->find();

		$res['user'] = db('user')->where(['id'=>$res['order']['user_id']])->field('avatarurl')->find();

		//评价信息
		
		$where['order_id'] = $post['id'];
        $where['admin_id'] = $this->accessData['aid'];

		$fields = ['content','grade','create_time'];
		$res['comment'] = db('xixiche_comment')->where($where)->field($fields)->find();
		if (!empty($res['comment']['create_time'])) {
			$res['comment']['create_time'] = date("Y-m-d H:i:s",$res['comment']['create_time']);
		}
		
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}

    /**
    * @SWG\Post(
    *   path="/member",
    *   tags={"User"},
    *   summary="用户端 - 我的月卡",
    *   description="用户端 - 我的月卡 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="expire", type="string", description="过期时间"),
    *       @SWG\Header(header="friends", type="string", description="次数"),
    *       @SWG\Header(header="member_card", type="string", description="月卡类型"),
    *       
    *       @SWG\Header(header="title", type="string", description="名字"),
    *       @SWG\Header(header="money", type="string", description="价格"),
    *       @SWG\Header(header="num", type="string", description="次数"),
    *       @SWG\Header(header="type", type="string", description="月卡类型：{1=月卡，2=季卡，3=年卡}"),
    *   )
    * )
    */
	public function member()
	{
		$info = db('user')->where(['id'=>$this->accessData['uid']])->field('expire,friends,member_card,status')->find();
		if (!empty($info['expire'])) {
			$info['expire'] = date("Y-m-d",$info['expire']);
		}
		if ($info['status'] == 0) {
			$res = db('xixiche_member')->where(['admin_id'=>$this->accessData['aid']])->field('id,title,money,num,time')->select();
			if ($res) {
				$infos['member'] = $res;
				$infos['status'] = 0;
				suc('', $infos);
			}else{
				err('没有数据');
			}
		}elseif ($info['status'] == 1) {
			suc('', $info);
		}
	}

    /**
    * @SWG\Post(
    *   path="/member",
    *   tags={"User"},
    *   summary="用户端 - 月卡续费",
    *   description="用户端 - 月卡续费 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="title", type="string", description="名字"),
    *       @SWG\Header(header="money", type="string", description="价格"),
    *       @SWG\Header(header="num", type="string", description="次数"),
    *       @SWG\Header(header="type", type="string", description="月卡类型：{1=月卡，2=季卡，3=年卡}"),
    *   )
    * )
    */
	public function card()
	{
		$res = db('xixiche_member')->where(['admin_id'=>$this->accessData['aid']])->field('id,title,money,num,time')->select();

		suc('', $res);
	}

    /**
    * @SWG\Post(
    *   path="/mobile",
    *   tags={"User"},
    *   summary="用户端 - 客服中心",
    *   description="用户端 - 客服中心 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="mobile", type="string", description="客服电话"),
    *   )
    * )
    */
	public function mobile()
	{
		$res = db('xixiche_company')->where(['id'=>1])->field('mobile')->find();
		suc('', $res);
	}

    /**
    * @SWG\Post(
    *   path="/about",
    *   tags={"User"},
    *   summary="用户端 - 公司地址",
    *   description="用户端 - 公司地址 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="address", type="string", description="地址"),
    *       @SWG\Header(header="longitude", type="string", description="经度"),
    *       @SWG\Header(header="latitude", type="string", description="纬度"),
    *   )
    * )
    */
	public function about()
	{
		$res['company'] = db('xixiche_company')->where(['id'=>1])->field('address,longitude,latitude')->find();

		$field = ['id,title,thumb,status,money'];
		$res['project'] = db('xixiche_project')->where(['admin_id'=>$this->accessData['aid']])->field($field)->order('sort desc')->select();

		suc('', $res);
	}

	//生成订单号码
	function get_sn() 
	{
		return date('YmdHis').$this->accessData['aid'].$this->accessData['uid'].rand(10,99);
	}

    /**
    * @SWG\Post(
    *   path="/pay_order",
    *   tags={"User"},
    *   summary="用户端 - 订单支付",
    *   description="用户端 - 订单支付 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="goods_id", description="商品ID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="goods_type", description="商品类型：{1=月卡，2=项目}", in="formData", required=true, type="string"),
    *
    *   @SWG\Parameter(name="goods_id", description="商品ID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="goods_type", description="商品类型：{1=月卡，2=项目}", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="car_id", description="汽车ID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="address", description="地址", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="time", description="预约时间", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="name", description="联系人", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="gender", description="性别", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="mobile", description="联系方式", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="status", description="项目类型:{1=上门(付费或月卡)，2=到店(不需付费)}", in="formData", required=true, type="string"),
    *
    * 
    *   
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="expire", type="string", description="过期时间"),
    *       @SWG\Header(header="friends", type="string", description="次数"),
    *       @SWG\Header(header="member_card", type="string", description="月卡类型"),
    *       
    *       @SWG\Header(header="title", type="string", description="名字"),
    *       @SWG\Header(header="money", type="string", description="价格"),
    *       @SWG\Header(header="num", type="string", description="次数"),
    *       @SWG\Header(header="type", type="string", description="月卡类型：{1=月卡，2=季卡，3=年卡}"),
    *   )
    * )
    */
	public function pay_order()
	{
		// goods_type =（1:购买月卡，2:商品）
		$post = Request::instance()->param();
			if (empty($post['goods_type'])) {
				err('请提交商品类型 goods_type:{1=月卡，2=项目}');
			}
			if (empty($post['goods_id'])) {
				err('请提交商品ID');
			}
			// 查询用户是否拥有月卡
			$where['id'] = $this->accessData['uid'];
			$where['status'] = 1;
			$member = db('user')->where($where)->find();

            /*王昊:
                无论月卡、季卡，所剩余的次数都折成15元每次，升级后重置天数
                我:
                好的 明白
                王昊:
                比如季卡剩4次，想升级年卡，1688-4*15=1628即可升级，天数重置为365
            */
			if ($post['goods_type'] == 1) {
					//查询月卡信息
					$msg = db('xixiche_member')->where(['id'=>$post['goods_id']])->find();

					// order表
					$data['admin_id'] = $this->accessData['aid'];                         
					$data['user_id'] = $this->accessData['uid'];                          
					$data['subject'] = '斑马汽车服务';      
					$data['type'] = 2;
                    if ($member['status'] == 1) {
                        $data['amount'] = $msg['money']-($member['friends']*0.01);
                        if ($data['amount'] < 0.01) {
                            $data['amount'] = 0.01;
                        }
                    }else{
                        $data['amount'] = $msg['money'];
                    }
					$data['shops_id'] = $post['goods_id'];                              
					$data['genre'] = 'xixiche-order';
					//创建订单
					$weixin = new WeixinLogic();
					$res = $weixin->unifiedOrder($data);

					if ($res) {
						// xixiche_order表
						$info['admin_id'] = $this->accessData['aid'];
						$info['user_id'] = $this->accessData['uid'];
						$info['pay_type'] = 2;
						$info['order_type'] = 1;
						$info['order_id'] = $res['orderId'];
						$info['goods_id'] = $post['goods_id'];
						$info['title'] = $msg['title'];
                        if ($member) {
                            $info['money'] = $msg['money']-($member['friends']*0.01);
                            if ($info['money'] < 0.01) {
                                $info['money'] = 0.01;
                            }
                        }else{
                            $info['money'] = $msg['money'];
                        }

                        $info['create_time'] = time();

						$result = db('xixiche_order')->insert($info);
						if ($result) {
							$res['status'] = 1;
							suc('购买成功', $res);
						}else{
							err('没有数据');
						}
					}
			}elseif ($post['goods_type'] == 2) {
				//查询项目信息
				$msg = db('xixiche_project')->where(['id'=>$post['goods_id']])->find();
				// 如果为上门洗车
				if ($post['status'] == 1) {
                    $search = strstr($post['address'],"开福区",false);
                    if (empty($search)) {
                        err('该位置不在服务区内');
                    }
					//如果有会员卡
					if ($member['friends'] > 0 && $member['expire'] >= time()) {

						$buy = db('user')->where(['id'=>$this->accessData['uid']])->setDec('friends');
						if ($buy) {
							// xixiche_order
							$info['admin_id'] = $this->accessData['aid'];
							$info['user_id'] = $this->accessData['uid'];
							$info['order_id'] = $this->get_sn();
							$info['car_id'] = $post['car_id'];
							$info['goods_id'] = $post['goods_id'];
							$info['title'] = $msg['title'];
							$info['name'] = $post['name'];
							$info['gender'] = $post['gender'];
							$info['mobile'] = $post['mobile'];
                            
							$info['address'] = $post['address'];
							$info['money'] = $msg['money'];
							$info['time'] = strtotime($post['time']);
                            $info['hour'] = $post['hour'];
							$info['order_type'] = 2;
							$info['pay_type'] = 1;
							$info['create_time'] = time();
							$info['status'] = 1;
							db('xixiche_order')->insert($info);
							$friends = db('user')->where(['id'=>$this->accessData['uid']])->find();
							if ($friends['friends'] == 0) {
								$result = db('user')->where(['id'=>$this->accessData['uid']])->update(['status'=>0]);
								if ($result) {
									// xixiche_order
									$res['status'] = 2;
									suc('购买成功2', $res);
								}else{
									err('没有数据');
								}
							}else{
								$res['status'] = 2;
								suc('购买成功1', $res);
							}
						}else{
							err('购买失败');
						}
					}else{
						// order表
						$data['admin_id'] = $this->accessData['aid'];                         
						$data['user_id'] = $this->accessData['uid'];                          
						$data['subject'] = '斑马汽车服务';      
						$data['type'] = 2;                       
						$data['amount'] = $msg['money'];
						$data['shops_id'] = $post['goods_id'];                              
						$data['genre'] = 'xixiche-order';
						
						//创建订单
						$weixin = new WeixinLogic();
						$res = $weixin->unifiedOrder($data);

						if ($res) {
							//	xixiche_order表
							$info['admin_id'] = $this->accessData['aid'];
							$info['user_id'] = $this->accessData['uid'];
							$info['order_id'] = $res['orderId'];
							$info['car_id'] = $post['car_id'];
							$info['goods_id'] = $post['goods_id'];
							$info['title'] = $msg['title'];
							$info['name'] = $post['name'];
							$info['gender'] = $post['gender'];
							$info['mobile'] = $post['mobile'];
							$info['address'] = $post['address'];
                            
							$info['money'] = $msg['money'];
							$info['time'] = strtotime($post['time']);
                            $info['hour'] = $post['hour'];
							$info['order_type'] = 2;
							$info['pay_type'] = 2;
							$info['create_time'] = time();
							$result = db('xixiche_order')->insert($info);
							if ($result) {
								$res['status'] = 1;
								suc('购买成功3', $res);
							}else{
								err('没有数据');
							}
						}
					}
				// 如果为到店服务
				}elseif ($post['status'] == 2) {
					//	xixiche_order表
					$info['admin_id'] = $this->accessData['aid'];
					$info['user_id'] = $this->accessData['uid'];
					$info['order_id'] = $this->get_sn();
					$info['car_id'] = $post['car_id'];
					$info['goods_id'] = $post['goods_id'];
					$info['title'] = $msg['title'];
					$info['name'] = $post['name'];
					$info['gender'] = $post['gender'];
					$info['mobile'] = $post['mobile'];
					$info['address'] = $post['address'];
					$info['money'] = $msg['money'];
					$info['time'] = strtotime($post['time']);
                    $info['hour'] = $post['hour'];
					$info['order_type'] = 2;
					$info['pay_type'] = 3;
					$info['status'] = 1;
					$info['create_time'] = time();
					$result = db('xixiche_order')->insert($info);
					if ($result) {
						$res['status'] = 3;
						suc('预定成功', $res);
					}else{
						err('没有数据');
					}
				}

			}
	}

	public function order_status($orderId)
	{
		db('xixiche_order')->where(['order_id'=>$orderId])->update(['status'=>1]);
        //查询订单信息
        $res = db('xixiche_order')->where(['order_id'=>$orderId])->find();
        //查询月卡信息
        $msg = db('xixiche_member')->where(['id'=>$res['goods_id']])->find();
        // 查询用户是否拥有月卡
        $where['id'] = $res['user_id'];
        $where['status'] = 1;
        $member = db('user')->where($where)->find();

        if ($res['order_type'] == 1) {
            if ($member) {
                // user表
                $user['member_card'] = $msg['id'];    //月卡类型
                $user['update_time'] = time();
                $user['friends'] = $msg['num'];   //使用次数
                $user['expire'] = time()+$msg['time']*86400; //过期时间
                $user['status'] = 1;
                db('user')->where(['id'=>$res['user_id']])->update($user);
            }else{
                $user['member_card'] = $msg['id'];
                $user['update_time'] = time();
                $user['friends'] = $msg['num'];
                $user['expire'] = time()+$msg['time']*86400; //过期时间
                $user['status'] = 1;
                db('user')->where(['id'=>$res['user_id']])->update($user);
            }
        }
                        

	}
	
    /**
    * @SWG\Post(
    *   path="/car_info",
    *   tags={"User"},
    *   summary="用户端 - 我的爱车",
    *   description="用户端 - 我的爱车 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="车ID"),
    *       @SWG\Header(header="title", type="string", description="汽车品牌型号"),
    *       @SWG\Header(header="number", type="string", description="车牌"),
    *       @SWG\Header(header="color", type="string", description="颜色"),
    *   )
    * )
    */
	public function car_info()
	{
		$res = db('xixiche_car')->where(['user_id'=>$this->accessData['uid']])->field('id,title,number,color')->select();
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}

    //按拼音排序
    public function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }
    
    public function paixu($Singer){
        foreach ($Singer as $key => $value) {
            if ($value['name']=='A') {
                $Singer1['A'][$key+1] = $value;
            }elseif ($value['name']=='B') {
                $Singer1['B'][$key+1] = $value;
            }elseif ($value['name']=='C') {
                $Singer1['C'][$key+1] = $value;
            }elseif ($value['name']=='D') {
                $Singer1['D'][$key+1] = $value;
            }elseif ($value['name']=='E') {
                $Singer1['E'][$key+1] = $value;
            }elseif ($value['name']=='F') {
                $Singer1['F'][$key+1] = $value;
            }elseif ($value['name']=='G') {
                $Singer1['G'][$key+1] = $value;
            }elseif ($value['name']=='H') {
                $Singer1['H'][$key+1] = $value;
            }elseif ($value['name']=='I') {
                $Singer1['I'][$key+1] = $value;
            }elseif ($value['name']=='J') {
                $Singer1['J'][$key] = $value;
            }elseif ($value['name']=='K') {
                $Singer1['K'][$key+1] = $value;
            }elseif ($value['name']=='L') {
                $Singer1['L'][$key+1] = $value;
            }elseif ($value['name']=='O') {
                $Singer1['O'][$key+1] = $value;
            }elseif ($value['name']=='P') {
                $Singer1['P'][$key+1] = $value;
            }elseif ($value['name']=='Q') {
                $Singer1['Q'][$key+1] = $value;
            }elseif ($value['name']=='R') {
                $Singer1['R'][$key+1] = $value;
            }elseif ($value['name']=='S') {
                $Singer1['S'][$key+1] = $value;
            }elseif ($value['name']=='T') {
                $Singer1['T'][$key+1] = $value;
            }elseif ($value['name']=='U') {
                $Singer1['U'][$key+1] = $value;
            }elseif ($value['name']=='V') {
                $Singer1['V'][$key+1] = $value;
            }elseif ($value['name']=='W') {
                $Singer1['W'][$key+1] = $value;
            }elseif ($value['name']=='X') {
                $Singer1['X'][$key+1] = $value;
            }elseif ($value['name']=='Y') {
                $Singer1['Y'][$key+1] = $value;
            }elseif ($value['name']=='Z') {
                $Singer1['Z'][$key+1] = $value;
            }
        }
    
        return $Singer1;
    }


    /**
    * @SWG\Post(
    *   path="/car_list",
    *   tags={"User"},
    *   summary="用户端 - 选择车型",
    *   description="用户端 - 选择车型 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="cate_id", description="品牌ID", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="汽车ID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="汽车品牌ID"),
    *       @SWG\Header(header="name", type="string", description="汽车品牌缩写"),
    *       @SWG\Header(header="title", type="string", description="汽车品牌名称"),
    *       @SWG\Header(header="image", type="string", description="汽车品牌图标"),
    *       
    *       @SWG\Header(header="id", type="string", description="汽车ID"),
    *       @SWG\Header(header="model", type="string", description="汽车型号"),
    *   )
    * )
    */
	public function car_list()
	{
		$post = Request::instance()->param();
		if (isset($post['cate_id'])) {
			$res = db('car_new')->where(['bannd_id'=>$post['cate_id']])->field('id,model,model_img')->select();
			if ($res) {
				suc('', $res);
			}else{
				err('没有数据');
			}
		}else{
			$Singer = db('car_brand')->field('id,name,title,image')->order('sort desc')->select();

			$Singer = $this->my_sort($Singer,'name',SORT_ASC,SORT_STRING);
			$res = $this->paixu($Singer);
			if ($res) {
				suc('', $res);
			}else{
				err('没有数据');
			}

		}
	}
    /**
    * @SWG\Post(
    *   path="/car_add",
    *   tags={"User"},
    *   summary="用户端 - 添加爱车",
    *   description="用户端 - 添加爱车 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="title", description="汽车品牌型号", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="number", description="车牌", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="color", description="颜色", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *   )
    * )
    */
	public function car_add()
	{
		$post = request()->only(['title','number','color'],'post');
		if (empty($post['title'])) {
			err('请选择车型');
		}elseif (empty($post['number'])) {
			err('请填写车牌');
		}elseif (empty($post['color'])) {
			err('请填写颜色');
		}
		$post['admin_id'] = $this->accessData['aid'];
		$post['user_id'] = $this->accessData['uid'];

		$post['create_time'] = time();
		$res = db('xixiche_car')->insert($post);
		if ($res) {
			suc('提交成功');
		}else{
			err('提交失败');
		}
	}
    /**
    * @SWG\Post(
    *   path="/payment_info",
    *   tags={"User"},
    *   summary="用户端 - 未完成订单",
    *   description="用户端 - 未完成订单 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="订单ID"),
    *       @SWG\Header(header="order_id", type="string", description="订单号"),
    *       @SWG\Header(header="title", type="string", description="服务内容"),
    *       @SWG\Header(header="time", type="string", description="预约时间"),
    *       @SWG\Header(header="money", type="string", description="价格"),
    *       @SWG\Header(header="pay_type", type="string", description="支付类型"),
    *   )
    * )
    */
	public function payment_info()
	{
        $post = request()->only(['num','limit'], 'post');
		$where['status'] = [['=','1'],['=','2'],['=','3'],'or'];
        $where['order_type'] = 2;
        $where['pay_type'] = [['=','1'],['=','2'],'or'];
		$where['admin_id'] = $this->accessData['aid'];
		$where['user_id'] = $this->accessData['uid'];
		$field = ['id','order_id','title','time','hour','money','pay_type'];
		$res = db('xixiche_order')->where($where)->field($field)->order('time desc')->limit($post['num'], $post['limit'])->select();
        foreach ($res as $key => $val) {
            $res[$key]['time'] = date("Y-m-d", $val['time']) . " " . $val['hour'];
        }
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}

	public function order_update()
	{
		$post = request()->only(['id'],'post');

		$res = db('xixiche_order')->where(['id'=>$post['id']])->update(['status'=>6]);
		if ($res) {
			suc('删除成功');
		}else{
			err('删除失败');
		}
	}

    /**
    * @SWG\Post(
    *   path="/paid_info",
    *   tags={"User"},
    *   summary="用户端 - 已完成订单",
    *   description="用户端 - 已完成订单 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="id", type="string", description="订单ID"),
    *       @SWG\Header(header="order_id", type="string", description="订单号"),
    *       @SWG\Header(header="title", type="string", description="服务内容"),
    *       @SWG\Header(header="time", type="string", description="预约时间"),
    *       @SWG\Header(header="money", type="string", description="价格"),
    *       @SWG\Header(header="pay_type", type="string", description="支付类型"),
    *   )
    * )
    */
	public function paid_info()
	{
        $post = request()->only(['num','limit'], 'post');
		$where['status'] = 4;
        $where['order_type'] = 2;
		$where['admin_id'] = $this->accessData['aid'];
		$where['user_id'] = $this->accessData['uid'];
		$field = ['id','order_id','title','time','hour','money','pay_type'];
		$res = db('xixiche_order')->where($where)->field($field)->order('end_time desc')->limit($post['num'], $post['limit'])->select();
        
        foreach ($res as $key => $val) {
            $res[$key]['time'] = date("Y-m-d", $val['time']) . " " . $val['hour'];
        }
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}

    /**
    * @SWG\Post(
    *   path="/order_details_info",
    *   tags={"User"},
    *   summary="用户端 - 订单详情",
    *   description="用户端 - 订单详情 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="订单ID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="order_status", type="string", description="订单状态：{1=预约成功，2=安排技师前往，3=洗车前，4=洗车后}"),
    *       @SWG\Header(header="order_time", type="string", description="预约时间"),
    *       @SWG\Header(header="order_going_time", type="string", description="安排技师前往时间"),
    *       @SWG\Header(header="order_start_time", type="string", description="洗车前时间"),
    *       @SWG\Header(header="order_end_time", type="string", description="洗车后时间"),
    *       @SWG\Header(header="order_start_img", type="string", description="洗车前照片"),
    *       @SWG\Header(header="order_end_img", type="string", description="洗车后照片"),
    *       
    *       @SWG\Header(header="userinfo_nickname", type="string", description="师傅姓名"),
    *       @SWG\Header(header="userinfo_avatarurl", type="string", description="师傅头像"),
    *       @SWG\Header(header="userinfo_grade", type="string", description="师傅星级"),
    *       @SWG\Header(header="userinfo_mobile", type="string", description="师傅电话"),
    *       
    *       @SWG\Header(header="comment_content", type="string", description="评价内容"),
    *       @SWG\Header(header="comment_grade", type="string", description="星级"),
    *       @SWG\Header(header="comment_create_time", type="string", description="评价时间"),
    *   )
    * )
    */
	public function order_details_info()
	{
		$post = request()->only(['id'],'post');

		//订单信息
		$where['id'] = $post['id'];
		$field = ['id','status','time','going_time','start_time','hour','end_time','start_img','end_img','user_info_id','order_id'];
		$res['order'] = db('xixiche_order')->where($where)->field($field)->find();
		$res['order']['time'] = date("Y-m-d",$res['order']['time'])." ".$res['order']['hour'];
		if (!empty($res['order']['going_time'])) {
			$res['order']['going_time'] = date("Y-m-d H:i",$res['order']['going_time']);
		}
		if (!empty($res['order']['start_time'])) {
			$res['order']['start_time'] = date("Y-m-d H:i",$res['order']['start_time']);
		}
		if (!empty($res['order']['end_time'])) {
			$res['order']['end_time'] = date("Y-m-d H:i",$res['order']['end_time']);
		}
		$res['order']['start_img'] = json_decode($res['order']['start_img']);
		$res['order']['end_img'] = json_decode($res['order']['end_img']);

		//洗车师傅
		$res['userinfo'] = db('user')->where(['id'=>$res['order']['user_info_id']])->field('nickname,avatarurl,grade,mobile')->find();
        if ($res['userinfo']['grade'] > 5) {
            $res['userinfo']['grade'] = 5;
        }
		//评价
		$wheres['order_id'] = $res['order']['id'];
		$wheres['admin_id'] = $this->accessData['aid'];
		$fields = ['content','grade','create_time'];
        
		$res['comment'] = db('xixiche_comment')->where($wheres)->field($fields)->find();
		if (empty($res['comment'])) {
			$res['comment'] = 1;
		}else{
			$res['comment'] = 2;
		}
		// if (!empty($res['comment']['create_time'])) {
		// 	$res['comment']['create_time'] = date("Y-m-d H:i:s",$res['comment']['create_time']);
		// }
		if ($res) {
			suc('', $res);
		}else{
			err('没有数据');
		}
	}
    /**
    * @SWG\Post(
    *   path="/comment",
    *   tags={"User"},
    *   summary="用户端 - 提交评价",
    *   description="用户端 - 提交评价 - 黎勇斌",
    *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
    *   @SWG\Parameter(name="id", description="订单ID", in="formData", required=true, type="string"),
    *   @SWG\Response(
    *       response=200,
    *       description="成功返回数据",
    *       @SWG\Header(header="order_status", type="string", description="订单状态：{1=预约成功，2=安排技师前往，3=洗车前，4=洗车后}"),
    *       @SWG\Header(header="order_time", type="string", description="预约时间"),
    *       @SWG\Header(header="order_going_time", type="string", description="安排技师前往时间"),
    *       @SWG\Header(header="order_start_time", type="string", description="洗车前时间"),
    *       @SWG\Header(header="order_end_time", type="string", description="洗车后时间"),
    *       @SWG\Header(header="order_start_img", type="string", description="洗车前照片"),
    *       @SWG\Header(header="order_end_img", type="string", description="洗车后照片"),
    *       
    *       @SWG\Header(header="userinfo_nickname", type="string", description="师傅姓名"),
    *       @SWG\Header(header="userinfo_avatarurl", type="string", description="师傅头像"),
    *       @SWG\Header(header="userinfo_grade", type="string", description="师傅星级"),
    *       @SWG\Header(header="userinfo_mobile", type="string", description="师傅电话"),
    *       
    *       @SWG\Header(header="comment_content", type="string", description="评价内容"),
    *       @SWG\Header(header="comment_grade", type="string", description="星级"),
    *       @SWG\Header(header="comment_create_time", type="string", description="评价时间"),
    *   )
    * )
    */
	public function comment()
	{
		$post = request()->only(['id','content','grade'],'post');

		if (empty($post['id'])) {
			err('请提交订单ID');
		}elseif (empty($post['content'])) {
			err('请填写内容');
		}elseif (empty($post['grade'])) {
			err('请填写评分');
		}

		$order = db('xixiche_order')->where(['id'=>$post['id']])->find();

		$data['order_id'] = $post['id'];
		$data['admin_id'] = $this->accessData['aid'];
		$data['user_id'] = $this->accessData['uid'];
		$data['user_info_id'] = $order['user_info_id'];
		$data['content'] = $post['content'];
		$data['grade'] = $post['grade'];
		$data['create_time'] = time();
		$res = db('xixiche_comment')->insert($data);
		if ($res) {
			suc('提交成功');
		}else{
			err('提交失败');
		}
	}

    public function car_del()
    {
        $post = request()->only(['id'], 'post');

        $res = db('xixiche_car')->where(['id'=>$post['id']])->delete();

        if ($res) {
            suc('删除成功');
        }else{
            err('删除失败');
        }
    }
}

